<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MySQL安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MySQL安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;平时安装mysql都是源码包安装的，但是由于它的编译需要很长的时间，所以建议使用二进制免编译包安装。可以到<a href="http://dev.mysql.com/downloads/" target="_blank" rel="external">MySQL官方网站</a>去下载，具体版本根据平台和需求而定，目前比较常用的为mysql-5.0/mysql-5.1，5.5和5.7版本虽然已经发布有段日子了，但是在线上跑服务器的还是少数。所以本文安装5.1的版本。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装以前一定要知道Linux系统是多少位。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">uname -i</div><div class="line">i386</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;i386其实就是32位，需要下载i686的包，如果结果为x86_64，则为64位。</p>
<h2 id="1、下载mysql到-usr-local-src"><a href="#1、下载mysql到-usr-local-src" class="headerlink" title="1、下载mysql到/usr/local/src/"></a>1、下载mysql到/usr/local/src/</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/</div><div class="line">wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz</div></pre></td></tr></table></figure>
<h2 id="2、解压"><a href="#2、解压" class="headerlink" title="2、解压"></a>2、解压</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># tar zxvf /usr/local/src/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz</span></div></pre></td></tr></table></figure>
<h2 id="3、把解压完的数据移动到-usr-local-mysql"><a href="#3、把解压完的数据移动到-usr-local-mysql" class="headerlink" title="3、把解压完的数据移动到/usr/local/mysql"></a>3、把解压完的数据移动到/usr/local/mysql</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql</span></div></pre></td></tr></table></figure>
<h2 id="4、建立mysql用户"><a href="#4、建立mysql用户" class="headerlink" title="4、建立mysql用户"></a>4、建立mysql用户</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># useradd -s /sbin/nologin mysql</span></div></pre></td></tr></table></figure>
<h2 id="5、初始化数据库"><a href="#5、初始化数据库" class="headerlink" title="5、初始化数据库"></a>5、初始化数据库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># cd /usr/local/mysql</span></div><div class="line">[root@localhost mysql]<span class="comment"># mkdir -p /data/mysql ; chown -R mysql:mysql /data/mysql</span></div><div class="line">[root@localhost mysql]<span class="comment"># ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <code>--user</code> 定义数据库的所属主， <code>--datadir</code> 定义数据库安装到哪里，建议放到大空间的分区上，这个目录需要自行创建。这一步很关键，如果看到两个“OK”，说明执行正确，否则请仔细查看错误信息。</p>
<h3 id="遇到错误1"><a href="#遇到错误1" class="headerlink" title="遇到错误1"></a>遇到错误1</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./bin/mysqld: error <span class="keyword">while</span> loading shared librarues:libstdc++.so.5:cannot open shared object file:</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y compat-libstdc++-33</div></pre></td></tr></table></figure>
<h3 id="遇到错误2"><a href="#遇到错误2" class="headerlink" title="遇到错误2"></a>遇到错误2</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./scripts/mysql_install_db: ./bin/my_print_defaults: /lib/ld-linux.so.2:bad ELF interpreter: NO such file or directory</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是因为，系统版本和mysql版本不一致。比如Linux系统是32位，mysql安装包是64位。所以解决办法是重新下载合适的安装包。</p>
<h3 id="依赖包的安装"><a href="#依赖包的安装" class="headerlink" title="依赖包的安装"></a>依赖包的安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel perl-Data-Dumper net-tools</div></pre></td></tr></table></figure>
<h2 id="6、拷贝配置文件"><a href="#6、拷贝配置文件" class="headerlink" title="6、拷贝配置文件"></a>6、拷贝配置文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># cp support-files/my-large.cnf /etc/my.cnf</span></div></pre></td></tr></table></figure>
<h2 id="7、拷贝启动脚本文件并修改其属性"><a href="#7、拷贝启动脚本文件并修改其属性" class="headerlink" title="7、拷贝启动脚本文件并修改其属性"></a>7、拷贝启动脚本文件并修改其属性</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># cp support-files/mysql.server /etc/init.d/mysqld</span></div><div class="line">[root@localhost mysql]<span class="comment"># chmod 755 /etc/init.d/mysqld</span></div></pre></td></tr></table></figure>
<h2 id="8、修改启动脚本"><a href="#8、修改启动脚本" class="headerlink" title="8、修改启动脚本"></a>8、修改启动脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># vim /etc/init.d/mysqld</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要修改的地方有 <code>datadir=/data/mysql</code> （之前初始化数据库时定义的目录）</p>
<h2 id="9、把启动脚本加入系统服务项，设定开机启动并启动mysql"><a href="#9、把启动脚本加入系统服务项，设定开机启动并启动mysql" class="headerlink" title="9、把启动脚本加入系统服务项，设定开机启动并启动mysql"></a>9、把启动脚本加入系统服务项，设定开机启动并启动mysql</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># chkconfig --add mysqld</span></div><div class="line">[root@localhost mysql]<span class="comment"># chkconfig mysqld on</span></div><div class="line">[root@localhost mysql]<span class="comment"># service mysqld start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果启动不了，请到 <code>/data/mysql/</code> 下查看错误日志，这个日志通常是 <code>主机名.err</code>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查mysql是否启动：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># ps aux |grep mysqld</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/1. Mysql安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
